<template>
  <div class="authority">
    <baleTable
      :tableData="tableData"
      :tableHead="tableHead"
      :isSearch="false"
      :isTableRowClick="true"
      :tableLoading="tableLoading"
      :page="page"
      @pageSizeChange="pageSizeChange"
      @handleCurrentChange="handleCurrentChange"
      @searchMethods="searchMethods"
      @tableSortChange="tableSortChange"
      @handleRowClick="handleRowClick"
      ref="baleTable"
    >
      <template #headLeft>
        <div
          class="ml10 tableStats-btn tableStats-btn-active"
          @click="showDialog"
        >
          添加
        </div>
      </template>

      <template #navigation="{ scope }">
        {{ scope.row.navigation ? "显示" : "隐藏" }}
      </template>
      <template #caozuo="{ scope }">
        <el-button type="primary" size="small" @click="editMenu(scope.row)"
          >编辑</el-button
        >
        <el-button type="danger" size="small" @click="delMenu(scope.row)"
          >删除</el-button
        >
      </template>
    </baleTable>
    <menuDialog ref="dialog" @changeList="getTableData"></menuDialog>
  </div>
</template>

<script>
import { Apimenu, ApimenuDel } from "@a";
import { tableHead } from "./config";
import menuDialog from "./component/menu_dialog.vue";
export default {
  components: {
    menuDialog,
  },
  data() {
    return {
      tableHead,
      tableData: [],
      page: {
        page: 1,
        size: 25,
        total: 0,
      },
      tableLoading: false,
      searchParams: {},
      sortParams: {},
    };
  },
  mounted() {
    this.getTableData();
  },
  methods: {
    // 删除
    delMenu(val) {
      this.$confirm("是否删除该角色?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        center: true,
        type: "warning",
      })
        .then((res) => {
          ApimenuDel(val.id).then((res) => {
            this.$message.success("操作成功！");
            this.getTableData();
          });
        })
        .catch((err) => {});
    },

    //编辑
    editMenu(val) {
      this.$refs.dialog.showDialog(val);
    },
    showDialog() {
      this.$refs.dialog.showDialog();
    },
    getTableData(params = {}) {
      let postParams = {
        ...params,
        ...this.searchParams,
        ...this.sortParams,
        ...this.page,
      };
      this.tableLoading = true;
      Apimenu(postParams).then((res) => {
        let { data, page } = res;
        this.tableData = data;
        this.page.total = page.total;

        this.tableLoading = false;
      });
    },
    searchMethods(params) {
      this.searchParams = {};
      this.page.page = 1;
      this.searchParams = params;
      this.getTableData();
    },
    pageSizeChange(val) {
      this.page.size = val;
      this.getTableData();
    },
    handleCurrentChange(val) {
      this.page.page = val;
      this.getTableData();
    },
    tableSortChange(params) {
      if (params.sort_str) {
        this.sortParams = { ...this.sortParams, ...params };
      } else {
        delete this.sortParams.sort_str;
        delete this.sortParams.sort_order;
      }
      this.page.page = 1;
      this.getTableData();
    },
    handleRowClick(params) {},
  },
  filters: {},
};
</script>

<style lang="less" scoped>
.authority {
  height: 100%;
  margin: 10px;
}
</style>
